package com.nanhai.buddhist.college.module.collegebase.api.teacher;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanhai.buddhist.college.framework.common.enums.UserTypeEnum;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.module.collegebase.api.student.vo.UpdatePasswordVO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.dto.TeacherRespDTO;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.dto.TeacherUserRespDTO;
import com.nanhai.buddhist.college.module.collegebase.convert.teacherteachers.TeacherTeachersConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesTeacherDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentroll.StudentRollDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.teacherteachers.TeacherTeachersDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.classes.ClassesMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.classes.ClassesTeacherMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.colleges.CollegesMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.departments.DepartmentsMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.professions.ProfessionsMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.studentroll.StudentRollMapper;
import com.nanhai.buddhist.college.module.collegebase.service.teacherteachers.TeacherTeachersService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.module.collegebase.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class TeacherApiImpl implements TeacherApi {
    @Resource
    private TeacherTeachersService teacherTeachersService;

    @Resource
    private StudentRollMapper studentRollMapper;


    @Override
    public CommonResult<List<TeacherRespDTO>> getTeacherByIds(Collection<String> ids) {
        List<TeacherTeachersDO> teacherTeachersList = teacherTeachersService.getTeacherTeachersList(ids);
        return success(TeacherTeachersConvert.INSTANCE.convertList03(teacherTeachersList));
    }

    @Override
    public CommonResult<TeacherRespDTO> getTeacherById(String id) {
        TeacherTeachersDO teacherTeachersList = teacherTeachersService.getTeacherTeachers(id);
        return success(TeacherTeachersConvert.INSTANCE.convertDTO(teacherTeachersList));
    }

    @Override
    public CommonResult<TeacherUserRespDTO> getTeacherByUserName(String teacherUsername) {
        TeacherTeachersDO userByUsername = teacherTeachersService.getUserByUsername(teacherUsername);

        if (Objects.nonNull(userByUsername)) {
            TeacherUserRespDTO build = TeacherUserRespDTO.builder().teacherId(userByUsername.getId())
                    .userType(UserTypeEnum.TEACHER.getValue())
                    .password(userByUsername.getPassword())   // TODO 密码
                    .build();
            return success(build);
        } else {
            return success(null);

        }
    }

    @Override
    public CommonResult<List<TeacherRespDTO>> getTeacherListByStudentId(String id) {
        StudentRollDO studentRollDO = studentRollMapper.selectById(id);
        if (ObjectUtil.isNull(studentRollDO)) {
            return new CommonResult<>();
        }
        List<TeacherTeachersDO> teacherTeachersList = teacherTeachersService.getTeacherListByClassesId(studentRollDO.getClassesId());
        return success(TeacherTeachersConvert.INSTANCE.convertList03(teacherTeachersList));
    }

    @Override
    public CommonResult<Boolean> updatedStudentPassword(UpdatePasswordVO req) {
        teacherTeachersService.updatedStudentPassword(req);
        return CommonResult.success(true);
    }

    @Override
    public CommonResult<List<TeacherRespDTO>> getAllTeacherList() {
        List<TeacherTeachersDO> allTeacherList = teacherTeachersService.getAllTeacherList();
        return success(TeacherTeachersConvert.INSTANCE.convertList03(allTeacherList));
    }

    /*@Override
    public CommonResult<Set<String>> getTeacherBossUserId(String id) {
        return success(teacherTeachersService.getAdminUserId(id));
    }*/
}
